/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain one copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mikepenz.iconics.sample.provider;

import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;

import com.mikepenz.iconics.Iconics;
import com.mikepenz.iconics.IconicsDrawable;
import com.mikepenz.iconics.sample.ResourceTable;
import com.mikepenz.iconics.sample.item.IconItem;
import com.mikepenz.iconics.typeface.ITypeface;
import com.mikepenz.iconics.view.IconicsImageView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

/**
 * 侧滑对象类迭代器
 *
 * @since 2021-03-27
 */
public class GenericModelProvider extends BaseItemProvider {
    private List<ITypeface> list;
    private AbilitySlice slice;
    private int changepos = -1;
    private ShapeElement mShapeElement;
    private HashMap<Integer, IconicsDrawable> iconCache = new HashMap<>();

    /**
     * 建立对象传递参数
     *
     * @param list 数据集
     * @param slice 页面
     */
    public GenericModelProvider(List<ITypeface> list, AbilitySlice slice) {
        this.list = list;
        this.slice = slice;
        mShapeElement = new ShapeElement(slice.getContext(), ResourceTable.Graphic_text_shape_bg_white);
    }

    /**
     * 设置改变的索引
     *
     * @param changepos 索引
     */
    public void setChangepos(int changepos) {
        this.changepos = changepos;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        if (list != null && position >= 0 && position < list.size()) {
            return list.get(position);
        }
        return Collections.emptyList();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
        Component cpt;
        if (convertComponent == null) {
            cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_item_sample, null, false);
        } else {
            cpt = convertComponent;
        }
        ITypeface sampleItem = list.get(position);
        Text text = (Text) cpt.findComponentById(ResourceTable.Id_item_index);
        Text texttitle = (Text) cpt.findComponentById(ResourceTable.Id_text_title);
        Text textremark = (Text) cpt.findComponentById(ResourceTable.Id_text_remark);
        IconicsImageView imgleft = (IconicsImageView) cpt.findComponentById(ResourceTable.Id_img_left);
        Component component = (Component) cpt.findComponentById(ResourceTable.Id_component);
        text.setText(sampleItem.getIconCount() + "");
        texttitle.setText(sampleItem.getFontName() + "");
        textremark.setText(sampleItem.getVersion() + "-" + sampleItem.getAuthor() + "");

        if (changepos == position) {
            component.setVisibility(Component.VISIBLE);
            text.setBackground(mShapeElement);
            texttitle.setTextColor(new Color(Color.getIntColor("#F44336")));
            textremark.setTextColor(new Color(Color.getIntColor("#F44336")));
        } else {
            component.setVisibility(Component.HIDE);
            text.setBackground(new ShapeElement());
            texttitle.setTextColor(new Color(Color.getIntColor("#000000")));
            textremark.setTextColor(new Color(Color.getIntColor("#000000")));
        }
        initImg(sampleItem, imgleft, position);
        return cpt;
    }

    private void initImg(ITypeface sampleItem, IconicsImageView imgLeft, int position) {
        IconicsDrawable iconicsDrawable = iconCache.get(position);
        if (iconicsDrawable == null) {
            List<String> iconsString = sampleItem.getIcons();
            Random random = new Random();
            int index = random.nextInt(iconsString.size());
            iconicsDrawable = new IconicsDrawable(imgLeft, imgLeft.getContext(), iconsString.get(index));
            iconCache.put(position, iconicsDrawable);
        }
        imgLeft.setIcon(iconicsDrawable);
    }
}
